//fileio.cpp
#include "fileio.h"
FileIO::FileIO(QObject *parent)
    : QObject(parent),
      m_title_content(QString("fileio"))
{
}

FileIO::~FileIO()
{
}

void FileIO::read()
{
    if(m_file_source.isEmpty()) {
        return;
    }
    QFile file(m_file_source.toLocalFile());
    if(!file.exists()) {
        qWarning() << "Does not exits: " << m_file_source.toLocalFile();
        return;
    }
    if(file.open(QIODevice::ReadOnly)) {
        QTextStream stream(&file);
        m_file_content = stream.readAll();
        emit textChanged(m_file_content);
    }
}

void FileIO::write()
{
    if(m_file_source.isEmpty()) {
        return;
    }
    QFile file(m_file_source.toLocalFile());
    if(file.open(QIODevice::WriteOnly)) {
        QTextStream stream(&file);
        stream << m_file_content;
    }
}

QUrl FileIO::source() const
{
    return m_file_source;
}

QString FileIO::text() const
{
    return m_file_content;
}

void FileIO::setSource(QUrl source)
{
    if (m_file_source == source)
        return;

    m_file_source = source;
    emit sourceChanged(source);
}

void FileIO::setText(QString text)
{
    if (m_file_content == text)
        return;
    m_file_content = text;
    emit textChanged(text);
}
